<!-- src/views/information/information/detail.vue -->
<template>
  <div class="article-detail-page">
    <!-- 文章内容区域 -->
    <div class="article-container">
      <!-- 标题 -->
      <h1 class="article-title">{{ articleData.title }}</h1>

      <!-- 作者和发布时间 -->
      <div class="article-meta">
        <div class="author-info">
          <span class="author-name">{{ articleData.author || '未知作者' }}</span>
          <span class="publish-time">{{ formatTime(articleData.publishTime) }}</span>
        </div>
      </div>

      <!-- 内容 -->
      <div class="article-content" v-html="articleData.content"></div>

      <!-- 点赞区域 -->
      <div class="like-section">
        <el-button
            :type="isLiked ? 'primary' : 'default'"
            :icon="Star"
            circle
            @click="handleLike"
            :loading="likeLoading"
        >
          <el-icon><Star /></el-icon>
        </el-button>
        <span class="like-count">{{ articleData.likeNum || 0 }}</span>
      </div>

      <!-- 分割线 -->
      <div class="divider-section">
        <div class="divider-line"></div>
        <div class="divider-text">评论</div>
        <div class="divider-line"></div>
      </div>

      <!-- 评论列表 -->
      <div class="comments-section">
        <div v-if="commentsLoading" class="loading-comments">
          <el-skeleton :rows="3" animated />
        </div>

        <div v-else-if="comments.length === 0" class="empty-comments">
          <el-empty description="暂无评论" :image-size="100" />
        </div>

        <div v-else class="comments-list">
          <div
              v-for="comment in comments"
              :key="comment.commentId"
              class="comment-item"
          >
            <div class="comment-avatar">
              <el-avatar
                  :src="comment.avatar || '/api/placeholder/40/40'"
                  :size="40"
              >
                <el-icon><User /></el-icon>
              </el-avatar>
            </div>

            <div class="comment-content">
              <div class="comment-header">
                <span class="comment-author">{{ comment.nickName || comment.userName }}</span>
              </div>
              <div class="comment-text">{{ comment.comment }}</div>
              <div class="comment-time">{{ formatCommentTime(comment.createTime) }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { Star, User } from '@element-plus/icons-vue'
import { getInformation } from "@/api/information/information.js"

const route = useRoute()

// 响应式数据
const articleData = ref({})
const comments = ref([])
const isLiked = ref(false)
const likeLoading = ref(false)
const commentsLoading = ref(false)

// 获取文章详情
const getArticleDetail = async () => {
  const articleId = route.params.id
  try {
    const response = await getInformation(articleId)
    articleData.value = response.data || {}
  } catch (error) {
    console.error('获取文章详情失败:', error)
    ElMessage.error('获取文章详情失败')
  }
}

// 获取评论列表
const getComments = async () => {
  const articleId = route.params.id
  commentsLoading.value = true
  try {
    // 这里调用评论接口，需要根据实际后端接口调整
    const response = await getCommentList({ postId: articleId })
    comments.value = response.data || []
  } catch (error) {
    console.error('获取评论列表失败:', error)
    comments.value = []
  } finally {
    commentsLoading.value = false
  }
}

// 点赞处理
const handleLike = async () => {
  likeLoading.value = true
  try {
    if (isLiked.value) {
      // 取消点赞
      await cancelLike(route.params.id)
      articleData.value.likeNum = Math.max(0, (articleData.value.likeNum || 0) - 1)
    } else {
      // 点赞
      await addLike(route.params.id)
      articleData.value.likeNum = (articleData.value.likeNum || 0) + 1
    }
    isLiked.value = !isLiked.value
  } catch (error) {
    console.error('点赞操作失败:', error)
    ElMessage.error('操作失败，请重试')
  } finally {
    likeLoading.value = false
  }
}

// 格式化时间
const formatTime = (time) => {
  if (!time) return ''
  try {
    const date = new Date(time)
    return date.toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
    })
  } catch {
    return ''
  }
}

// 格式化评论时间
const formatCommentTime = (time) => {
  if (!time) return ''
  try {
    const date = new Date(time)
    const now = new Date()
    const diff = now - date

    // 1分钟内
    if (diff < 60000) {
      return '刚刚'
    }
    // 1小时内
    if (diff < 3600000) {
      return `${Math.floor(diff / 60000)}分钟前`
    }
    // 1天内
    if (diff < 86400000) {
      return `${Math.floor(diff / 3600000)}小时前`
    }
    // 1天内
    if (diff < 172800000) {
      return '昨天'
    }
    // 其他情况显示具体日期
    return date.toLocaleDateString('zh-CN', {
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
    })
  } catch {
    return ''
  }
}

// 初始化
onMounted(() => {
  getArticleDetail()
  getComments()
})
</script>

<style scoped>
.article-detail-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7ed 100%);
  padding: 20px;
}

.article-container {
  max-width: 800px;
  margin: 0 auto;
  background: white;
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.article-title {
  font-size: 2.2rem;
  color: #2c3e50;
  margin-bottom: 20px;
  line-height: 1.4;
  font-weight: 700;
  text-align: center;
}

.article-meta {
  margin-bottom: 30px;
  text-align: center;
}

.author-info {
  display: inline-flex;
  gap: 20px;
  color: #7f8c8d;
  font-size: 0.95rem;
}

.author-name {
  font-weight: 500;
}

.article-content {
  line-height: 1.8;
  font-size: 1.1rem;
  color: #2c3e50;
  margin-bottom: 40px;
}

.article-content :deep(p) {
  margin-bottom: 1.2em;
}

.article-content :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin: 1em 0;
}

.like-section {
  text-align: center;
  margin-bottom: 40px;
  padding: 20px 0;
  border-top: 1px solid #f0f0f0;
  border-bottom: 1px solid #f0f0f0;
}

.like-section .el-button {
  width: 60px;
  height: 60px;
  font-size: 1.5rem;
}

.like-count {
  display: block;
  margin-top: 12px;
  font-size: 1.1rem;
  color: #666;
  font-weight: 600;
}

.divider-section {
  display: flex;
  align-items: center;
  margin: 40px 0;
  gap: 20px;
}

.divider-line {
  flex: 1;
  height: 1px;
  background: linear-gradient(90deg, transparent, #ddd, transparent);
}

.divider-text {
  color: #7f8c8d;
  font-size: 1.2rem;
  font-weight: 600;
  padding: 0 20px;
}

.comments-section {
  margin-top: 30px;
}

.loading-comments {
  padding: 20px 0;
}

.empty-comments {
  padding: 40px 0;
}

.comments-list {
  space-y: 20px;
}

.comment-item {
  display: flex;
  gap: 16px;
  padding: 20px 0;
  border-bottom: 1px solid #f5f5f5;
}

.comment-item:last-child {
  border-bottom: none;
}

.comment-avatar {
  flex-shrink: 0;
}

.comment-content {
  flex: 1;
  min-width: 0;
}

.comment-header {
  margin-bottom: 8px;
}

.comment-author {
  font-weight: 600;
  color: #2c3e50;
  font-size: 0.95rem;
}

.comment-text {
  color: #333;
  line-height: 1.6;
  margin-bottom: 8px;
  word-break: break-word;
}

.comment-time {
  font-size: 0.85rem;
  color: #7f8c8d;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .article-detail-page {
    padding: 12px;
  }

  .article-container {
    padding: 24px;
  }

  .article-title {
    font-size: 1.8rem;
  }

  .author-info {
    flex-direction: column;
    gap: 8px;
  }

  .divider-section {
    margin: 30px 0;
  }

  .comment-item {
    padding: 16px 0;
  }
}
</style>